'use strict';

const Controller = require('egg').Controller;

class FollowController extends Controller {
    //关注
    async create() {
        let {ctx,service} = this;
        let {uid} = ctx.info;
        let {follow_id} = ctx.request.body;
        if(follow_id){
            try{
                let target = await service.follow.create(follow_id,uid);
                console.log("target",target);
                ctx.body = {
                    code:1,
                    msg:'关注成功',
                    id:target.insertId
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //取消关注
    async destroy(){
        let {ctx,service} = this;
        let {id} = ctx.params;

        if(id){
            try{
                await service.follow.destroy(id);
                ctx.body = {
                    code:1,
                    msg:'取消关注成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //查询关注成员列表
    async index(){
        let {ctx,service} = this;
        let {uid} = ctx.info;

        let followlist = await service.follow.index(uid);
        ctx.body = {
            code:1,
            data:followlist
        }
    }

    async show(){
        let {ctx,service} = this;
        let {uid} = ctx.info;
        let {id} = ctx.params; //id follow_id
        if(id){
            let person = await service.follow.show(uid,id);
            if(person.length){
                ctx.body = {
                    code:1,
                    msg:'此人已被关注',
                    id:person[0].id
                }
            }else{
                ctx.body = {
                    code:3,
                    msg:'此人没被关注'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
}

module.exports = FollowController;
